package com.htfyun.ft.fragment.engineer;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.htfyun.ft.R;
import com.htfyun.ft.agingtest.AgingTestConstants;
import com.htfyun.ft.agingtest.AgingTestItemAttr;
import com.htfyun.ft.utils.DebugLog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewAdapterAgingItems extends RecyclerView.Adapter<RecyclerViewAdapterAgingItems.NormalViewHolder> {

    private List<AgingTestItemAttr> agingTestItemList = new ArrayList<>();

    private final LayoutInflater mInflater;
    private final Context mContext;

    public RecyclerViewAdapterAgingItems(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        agingTestItemList.clear();
        agingTestItemList.addAll(Arrays.asList(AgingTestItemAttr.values()));
        Collections.sort(agingTestItemList);

    }
    @NonNull
    @Override
    public NormalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.recycler_item_aging_setting, parent, false);
        return new NormalViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull NormalViewHolder holder, int position) {
        AgingTestItemAttr itemAttr = agingTestItemList.get(position);

        holder.txtItem.setText(itemAttr.getNominalName());

        int descRes = R.string.aging_settings_count;
        long times = itemAttr.getTimesOfTestOver();
        if (!itemAttr.isCountForTestOver()) {
            descRes = R.string.aging_settings_time;
            times /= AgingTestConstants.ONE_MIN;
        }
        holder.txtTimes.setText(mContext.getString(descRes));
        holder.editTimes.setText(String.valueOf(times));
        holder.itemAttr = itemAttr;

        holder.setSwitchChecked(!itemAttr.isForceTestOver());
    }

    @Override
    public int getItemCount() {
        return agingTestItemList.size();
    }


    public static final class NormalViewHolder extends RecyclerView.ViewHolder
            implements CompoundButton.OnCheckedChangeListener, TextWatcher {
        private final TextView txtItem;
        private final Switch switchTestOver;

        private final TextView txtTimes;
        private final EditText editTimes;

        private AgingTestItemAttr itemAttr;

        private NormalViewHolder(View itemView) {
            super(itemView);
            txtItem = (TextView) itemView.findViewById(R.id.txtItem);
            switchTestOver = (Switch) itemView.findViewById(R.id.switchTestOver);
            switchTestOver.setOnCheckedChangeListener(this);


            txtTimes = (TextView) itemView.findViewById(R.id.txtTimes);
            editTimes = (EditText) itemView.findViewById(R.id.editTimes);

            editTimes.addTextChangedListener(this);
        }

        private void setSwitchChecked(boolean checked) {
            switchTestOver.setChecked(checked);
            setEnabledBySwitch(checked);
        }

        private void setEnabledBySwitch(boolean switchChecked) {
            txtTimes.setEnabled(switchChecked);
            editTimes.setEnabled(switchChecked);
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (itemAttr != null) {
                itemAttr.setForceTestOver(!isChecked);
            }

            DebugLog.e("isChecked = " + isChecked);

            setEnabledBySwitch(isChecked);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mHandler.removeCallbacks(mRunnable);
            //800毫秒没有输入认为输入完毕
            mHandler.postDelayed(mRunnable, DELAY_EDIT_OK);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }

        private final int MSG_EDIT_OK = 100;
        private final int DELAY_EDIT_OK = 500;
        private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == MSG_EDIT_OK) {
                    CharSequence str = editTimes.getText();
                    if (!TextUtils.isEmpty(str) && TextUtils.isDigitsOnly(str)) {
                        long times = Long.valueOf(str.toString());
                        if (itemAttr != null) {
                            if (!itemAttr.isCountForTestOver()) {
                                times *= AgingTestConstants.ONE_MIN;
                            }
                            itemAttr.setTimesOfTestOver(times);
                        }
                    }
                }
            }
        };
        private Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(MSG_EDIT_OK);
            }
        };
    }
}
